package cs236369.webserver;

import static org.junit.Assert.*;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;

import org.junit.Before;
import org.junit.Test;
import org.xml.sax.SAXException;


public class ParserTest {

	Parser testingParser = null;
	
	/**
	 * Initialize testingParser to hold the content of the config.xml
	 * @throws IOException 
	 * @throws SAXException 
	 * @throws ParserConfigurationException 
	 */
	@Before
	public void init() throws ParserConfigurationException, SAXException, IOException {
		
		// creating the InputStream
		FileInputStream fip = new FileInputStream("config.xml");
		testingParser = new Parser(fip);
	}
	
	@Test
	public void testNumSocketReaders() throws XPathExpressionException {
		
		int numReaders = testingParser.getNumSocketReaders();
		assertEquals(3,numReaders);
	}
	
	@Test
	public void testNumRequestReaders() throws XPathExpressionException {
		
		int numReaders = testingParser.getNumRequestReaders();
		assertEquals(7,numReaders);
	}
	
	@Test
	public void testGetBase() throws XPathExpressionException {
		
		String base = testingParser.getBaseDirectory();
		assertEquals(base, "C:\\www\\srv");
	}
	
	@Test
	public void testGetPort() throws XPathExpressionException {
		
		int port = testingParser.getPort();
		assertEquals(8082, port);
	}
	
	@Test
	public void testGetQueueSize() throws XPathExpressionException {
		
		int queueSize = testingParser.getNumSocketQueue();
		assertEquals(100, queueSize);
	}
	
	@Test
	public void testGetRequestQueue() throws XPathExpressionException {
		
		int queueSize = testingParser.getNumRequestQueue();
		assertEquals(200, queueSize);
	}
	
	@Test
	public void testTypeHandlers() throws XPathExpressionException {
		
		Map<String,String> handlers = testingParser.getExtensions();
		assertEquals(handlers.get("tsp"), "cs236369.webserver.TSPEngine");
		assertEquals(handlers.get("tsp"), "cs236369.webserver.TSPEngine");
	}
	
	@Test
	public void testPath() throws XPathExpressionException {
		String compilePath = testingParser.getCompilePath("cs236369.webserver.TSPEngine");
		assertEquals("C:\\Program Files\\Java\\jdk1.6.0_24\\", compilePath);
	}
	
	@Test 
	public void testGetWelcomeFile() throws XPathExpressionException {
		
		String welcome = testingParser.getWelcomeFile();
		assertEquals("index.html", welcome);
	}
	
	@Test
	public void testGetMimeType() throws XPathExpressionException {
		
		String contentType = testingParser.getMimeType("html");
		assertEquals("text/html", contentType);
		
		contentType = testingParser.getMimeType("asx");
		assertEquals("video/x-ms-asf", contentType);
	}
}
